#!/usr/bin/perl -w

#### Author:    Abbi @ Earthen Ring (US)
####            strand.osric@gmail.com
#### Version:   .1
#                       .1     first version number
#                       .2     fixed timestamps
#                       .21    minor debug changes
#                       .3     major 's fix (non-greedy regexps, Avenger's Shield)

use Time::Local;
use Getopt::Std;
use Data::Dumper;
use DBI;
use strict;

#### Usage:     -v (verbose)

my %opts;
getopts('v', \%opts);
my $debug = $opts{'v'};

#### Connect to the database

my $data_source = "dbi:SQLite:./log.db";
my $username = "";
my $auth = "";
my %attr;

my $dbh = DBI->connect($data_source, $username, $auth, \%attr);

#### TODO:

my %loglines = (
# DAMAGE: ts, ms, actor, target, skill, type, amount, element, blocked, absorbed, resisted
# type = crit, hit, crushing, glancing, shield, immune, parry, block, miss, dodge, absorb

    #### Melee damage

    '(?!.+\'s )(.+) (crit|hit)s (.+) for (\d+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, $4 ],
        )',
    '(?!.+\'s )(.+) (crit|hit)s (.+) for (\d+)\. \((\d+) absorbed\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "absorbed" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, $4, $5 ],
        )',
    '(.+) hits (.+) for (\d+)\. \(crushing\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount" ],
            "vals"    => [ $ts, $ms, $1, $2, "crushing", $3 ],
        )',
    '(.+) hits (.+) for (\d+)\. \(crushing\) \((\d+) absorbed\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "absorbed" ],
            "vals"    => [ $ts, $ms, $1, $2, "crushing", $3, $4 ],
        )',
    '(.+) hits (.+) for (\d+)\. \(glancing\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount" ],
            "vals"    => [ $ts, $ms, $1, $2, "glancing", $3 ],
        )',
    '(?!.+\'s )(.+) hits (.+) for (\d+)\. \((\d+) blocked\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "blocked" ],
            "vals"    => [ $ts, $ms, $1, $2, "hit", $3, $4 ],
        )',
    '(?!.+\'s )(.+) hits (.+) for (\d+)\. \((\d+) blocked\) \((\d+) absorbed\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "blocked", "absorbed" ],
            "vals"    => [ $ts, $ms, $1, $2, "hit", $3, $4, $5 ],
        )',

    #### Elemental melee damage

    '(?!.+\'s )(.+) (crit|hit)s (.+) for (\d+) (\w+) damage\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "element" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, $4, lc($5) ],
        )',
    '(?!.+\'s )(.+) (crit|hit)s (.+) for (\d+) (\w+) damage\. \((\d+) resisted\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "element", "resisted" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, $4, lc($5), $6 ],
        )',
    '(?!.+\'s )(.+) hits (.+) for (\d+) (\w+) damage\. \((\d+) blocked\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "element", "blocked" ],
            "vals"    => [ $ts, $ms, $1, $2, "hit", $3, lc($4), $5 ],
        )',
    '(?!.+\'s )(.+) hits (.+) for (\d+) (\w+) damage\.  \((\d+) resisted\) \((\d+) blocked\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "element", "resisted", "blocked" ],
            "vals"    => [ $ts, $ms, $1, $2, "hit", $3, lc($4), $5, $6, $7 ],
        )',
    '(?!.+\'s )(.+) hits (.+) for (\d+) (\w+) damage\. \(glancing\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "element" ],
            "vals"    => [ $ts, $ms, $1, $2, "glancing", $3, lc($4) ],
        )',
    '(?!.+\'s )(.+) hits (.+) for (\d+) (\w+) damage\. \(glancing\) \((\d+) resisted\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type", "amount", "element", "resisted" ],
            "vals"    => [ $ts, $ms, $1, $2, "glancing", $3, lc($4), $5 ],
        )',

    #### Skill damage

    '(.+?)\'s (.+) (crit|hit)s (.+) for (\d+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type", "amount" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, $3, $5 ],
        )',
    '(.+?)\'s (.+) hits (.+) for (\d+)\. \((\d+) blocked\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type", "amount", "blocked" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "hit", $4, $5 ],
        )',
    '(.+?)\'s (.+) hits (.+) for (\d+)\. \((\d+) absorbed\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type", "amount", "absorbed" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "hit", $4, $5 ],
        )',
    '(.+?)\'s (.+) hits (.+) for (\d+)\. \((\d+) blocked\) \((\d+) absorbed\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type", "amount", "blocked", "absorbed" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "hit", $4, $5, $6 ],
        )',

    #### Elemental skill damage

    '(.+?)\'s (.+) (crit|hit)s (.+) for (\d+) (\w+) damage\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type", "amount", "element" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, $3, $5, lc($6) ],
        )',
    '(.+?)\'s (.+) (crit|hit)s (.+) for (\d+) (\w+) damage\. \((\d+) resisted\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type", "amount", "element", "resisted" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, $3, $5, lc($6), $7 ],
        )',
    '(.+?)\'s (.+) hits (.+) for (\d+) (\w+) damage\. \((\d+) resisted\) \((\d+) blocked\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type", "amount", "element", "resisted", "blocked" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "hit", $4, lc($5), $6, $7 ],
        )',
    '(.+?)\'s (.+) (crit|hit)s (.+) for (\d+) (\w+) damage\. \((\d+) absorbed\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type", "amount", "element", "absorbed" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, $3, $5, lc($6), $7 ],
        )',
    '(.+?)\'s (.+) (crit|hit)s (.+) for (\d+) (\w+) damage\. \((\d+) resisted\) \((\d+) absorbed\)' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type", "amount", "element", "resisted", "absorbed" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, $3, $5, lc($6), $7, $8 ],
        )',

    #### Pain and suffering

    '(.+) falls and loses (\d+) health\.' =>  
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "target", "skill", "amount" ],
            "vals"    => [ $ts, $ms, $1, "Falling", $2 ],
        )',
    '(.+) suffers (\d+) (\w+) damage from (?!.+\'s )(.+)\.' =>  
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "target", "skill", "amount", "element" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, lc($3) ],
        )',
    '(.+) suffers (\d+) (\w+) damage from (?!.+\'s )(.+)\. \((\d+) resisted\)' =>  
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "target", "skill", "amount", "element" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, lc($3) ],
        )',
    '(.+) suffers (\d+) (\w+) damage from (?!.+\'s )(.+)\. \((\d+) absorbed\)' =>  
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "target", "skill", "amount", "element", "absorbed" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, lc($3), $5 ],
        )',
    '(.+) suffers (\d+) (\w+) damage from (?!.+\'s )(.+)\. \((\d+) resisted\) \((\d+) absorbed\)' =>  
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "target", "skill", "amount", "element", "resisted", "absorbed" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, lc($3), $5, $6 ],
        )',
    '(.+) suffers (\d+) (\w+) damage from (.+?)\'s (.+)\.' =>  
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "amount", "element" ],
            "vals"    => [ $ts, $ms, $4, $1, $5, $2, lc($3) ],
        )',
    '(.+) suffers (\d+) (\w+) damage from (.+?)\'s (.+)\. \((\d+) resisted\)' => 
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "amount", "element", "resisted" ],
            "vals"    => [ $ts, $ms, $4, $1, $5, $2, lc($3), $6 ],
        )',
    '(.+) suffers (\d+) (\w+) damage from (.+?)\'s (.+)\. \((\d+) absorbed\)' => 
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "amount", "element", "absorbed" ],
            "vals"    => [ $ts, $ms, $4, $1, $5, $2, lc($3), $6 ],
        )',
    '(.+) suffers (\d+) (\w+) damage from (.+?)\'s (.+)\. \((\d+) resisted\) \((\d+) absorbed\)' =>  
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "amount", "element", "resisted", "absorbed" ],
            "vals"    => [ $ts, $ms, $4, $1, $5, $2, lc($3), $6, $7 ],
        )',
    '(.+) reflects (\d+) (\w+) damage to (.+)\.' => 
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "amount", "element", "type" ],
            "vals"    => [ $ts, $ms, $1, $4, $2, lc($3), "reflect" ],
        )',

    #### Misses

    '(.+) attacks\. (.+) dodges\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type" ],
            "vals"    => [ $ts, $ms, $1, $2, "dodge" ],
        )',
    '(.+) attacks\. (.+) parries\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type" ],
            "vals"    => [ $ts, $ms, $1, $2, "parry" ],
        )',
    '(.+) attacks\. (.+) blocks\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type" ],
            "vals"    => [ $ts, $ms, $1, $2, "block" ],
        )',
    '(.+) misses (.+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type" ],
            "vals"    => [ $ts, $ms, $1, $2, "miss" ],
        )',
    '(.+?)\'s (.+) was resisted by (.+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "resist" ],
        )',
    '(.+?)\'s (.+) was resisted\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "skill", "type" ],
            "vals"    => [ $ts, $ms, $1, $2, "resist" ],
        )',
    '(?!.+\'s )(.+) is absorbed by (.+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $2, $1, "absorb" ],
        )',
    '(.+?)\'s (.+) is absorbed by (.+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "absorb" ],
        )',
    '(.+?)\'s (.+) was parried by (.+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "parry" ],
        )',
    '(.+?)\'s (.+) was blocked by (.+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "block" ],
        )',
    '(.+?)\'s (.+) was dodged by (.+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "dodge" ],
        )',
    '(.+?)\'s (.+) missed (.+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "miss" ],
        )',
    '(?!.+\'s )(.+) fails\. (.+) is immune\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $2, $1, "immune" ],
        )',
    '(.+?)\'s (.+) fails\. (.+) is immune\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $1, $3, $2, "immune" ],
        )',
    '(.+) attacks but (.+) is immune\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "type" ],
            "vals"    => [ $ts, $ms, $1, $2, "immune" ],
        )',
    '(.+) is immune to (.+?)\'s (.+)\.' =>
        'sqlize(
            "table"    => "damage",
            "cols"     => [ "ts", "ms", "actor", "target", "skill", "type" ],
            "vals"    => [ $ts, $ms, $1, $2, $3, "immune" ],
        )',

# STAT: ts, ms, actor, target, type, skill, amount, critical
# type = crit, hit

    '(.+?)\'s (.+) drains (?:\d+) (?:.+) from (.+)\. (?:.+) gains (\d+) (.+)\.' =>
        'sqlize(
            "table"     => "stat",
            "cols"      => [ "ts", "ms", "actor", "target", "type", "skill", "amount" ],
            "vals"      => [ $ts,  $ms,  $1,      $3,       $5,     $2,      $4 ],
        )',
    '(.+?)\'s (.+?) (critically )*heals (.+) for (\d+)\.' => 
        'sqlize(
            "table"     => "stat",
            "cols"      => [ "ts", "ms", "actor", "target", "type", "skill", "amount", "critical" ],
            "vals"      => [ $ts, $ms, $1, $4, "health", $2, $5, ($3 ? "1" : "0") ],
        )',
    '(.+) gains (\d+) (\w+) from (.+?)\'s (.+)\.' => 
        'sqlize(
            "table"     => "stat",
            "cols"      => [ "ts", "ms", "actor", "target", "type", "skill", "amount" ],
            "vals"      => [ $ts, $ms, $4, $1, lc($3), $5, $2 ],
        )',

# AURA: ts, ms, actor, target, name, value, type, skill
# type = gain, lose, static

    '(.+) gains ([^\d]+)\.' =>
        'sqlize(
            "table"     => "aura",
            "cols"      => [ "ts", "ms", "target", "name", "type" ],
            "vals"      => [ $ts, $ms, $1, $2, "gain" ],
        )',
    '(.+) gains (.+) \((\d+)\)\.' =>
        'sqlize(
            "table"     => "aura",
            "cols"      => [ "ts", "ms", "target", "name", "value", "type" ],
            "vals"      => [ $ts, $ms, $1, $2, $3, "gain" ],
        )',
    '(.+) is afflicted by (.+)\.' =>
        'sqlize(
            "table"     => "aura",
            "cols"      => [ "ts", "ms", "target", "name", "type" ],
            "vals"      => [ $ts, $ms, $1, $2, "gain" ],
        )',
    '(.+) fades from (.+)\.' =>
        'sqlize(
            "table"     => "aura",
            "cols"      => [ "ts", "ms", "target", "name", "type" ],
            "vals"      => [ $ts, $ms, $2, $1, "lose" ],
        )',
    '(.+?)\'s (.+) is removed\.' =>
        ,
        'sqlize(
            "table"     => "aura",
            "cols"      => [ "ts", "ms", "target", "name", "type" ],
            "vals"      => [ $ts, $ms, $1, $2, "lose" ],
        )',
    '(.+?)\'s (.+) is removed by (?!.+\'s )(.+)\.' =>
        'sqlize(
            "table"     => "aura",
            "cols"      => [ "ts", "ms", "actor", "target", "name", "type" ],
            "vals"      => [ $ts, $ms, $3, $1, $2, "lose" ],
        )',
    '(.+?)\'s (.+) is removed by (.+?)\'s (.+).' =>
        'sqlize(
            "table"     => "aura",
            "cols"      => [ "ts", "ms", "actor", "target", "name", "type", "skill" ],
            "vals"      => [ $ts, $ms, $3, $1, $2, "lose", $4 ],
        )',
    '(.+) fails to dispel (.+?)\'s (.+)\.' =>
        'sqlize(
            "table"     => "aura",
            "cols"      => [ "ts", "ms", "actor", "target", "name", "type" ],
            "vals"      => [ $ts, $ms, $1, $2, $3, "static" ],
        )',

# SKILL: ts, ms, actor, target, skill, owner, begin

    '(.+) casts (.+) on (.+?)\'s (.+)\.' =>
        'sqlize(
            "table"     => "skill",
            "cols"      => [ "ts", "ms", "actor", "target", "skill", "owner" ],
            "vals"      => [ $ts, $ms, $1, $4, $2, $3 ],
        )',
    '(.+) performs (.+) on (.+)\.' =>
        'sqlize(
            "table"     => "skill",
            "cols"      => [ "ts", "ms", "actor", "target", "skill" ],
            "vals"      => [ $ts, $ms, $1, $3, $2 ],
        )',
    '(.+) performs (?!.+on)(.+)\.' =>
        'sqlize(
            "table"     => "skill",
            "cols"      => [ "ts", "ms", "actor", "skill" ],
            "vals"      => [ $ts, $ms, $1, $2 ],
        )',
    '(.+) begins to cast (.+)\.' =>
        'sqlize(
            "table"     => "skill",
            "cols"      => [ "ts", "ms", "actor", "skill", "begin" ],
            "vals"      => [ $ts, $ms, $1, $2, 1 ],
        )',
    '(.+) begins to perform (.+)\.' =>
        'sqlize(
            "table"     => "skill",
            "cols"      => [ "ts", "ms", "actor", "skill", "begin" ],
            "vals"      => [ $ts, $ms, $1, $2, 1 ],
        )',
    '(.+) casts (?!.+on)(.+)\.' =>
        'sqlize(
            "table"     => "skill",
            "cols"      => [ "ts", "ms", "actor", "skill" ],
            "vals"      => [ $ts, $ms, $1, $2 ],
        )',

# MAKE: ts, ms, actor, item

    '(.+) creates (.+)\.' =>
        'sqlize(
            "table"     => "make",
            "cols"      => [ "ts", "ms", "actor", "item" ],
            "vals"      => [ $ts, $ms, $1, $2 ],
        )',

# DEATH: ts, ms, actor, target

    '(.+) dies\.' =>
        'sqlize(
            "table"    => "death",
            "cols"     => [ "ts", "ms", "target" ],
            "vals"    => [ $ts, $ms, $1 ],
        )',
    '(.+) is destroyed\.' =>
        'sqlize(
            "table"    => "death",
            "cols"     => [ "ts", "ms", "target" ],
            "vals"    => [ $ts, $ms, $1 ],
        )',
    '(.+) is slain by (.+)!' =>
        'sqlize(
            "table"    => "death",
            "cols"     => [ "ts", "ms", "actor", "target" ],
            "vals"    => [ $ts, $ms, $2, $1 ],
        )',
    '(.+) is killed by (.+)\.' =>
        'sqlize(
            "table"    => "death",
            "cols"     => [ "ts", "ms", "actor", "target" ],
            "vals"    => [ $ts, $ms, $2, $1 ],
        )',

# INTERRUPT: ts, ms, actor, target, skill

    '(.+) interrupts (.+?)\'s (.+)\.' =>
        'sqlize(
            "table"    => "interrupt",
            "cols"     => [ "ts", "ms", "actor", "target", "skill" ],
            "vals"    => [ $ts, $ms, $1, $2, $3 ],
        )',

# PET: ts, ms, actor, food

    '(.+)\'s pet begins eating a (.+)\.' =>
        'sqlize(
            "table"    => "pet",
            "cols"     => [ "ts", "ms", "actor", "food" ],
            "vals"    => [ $ts, $ms, $1, $2 ],
        )',

);

my $year = (localtime)[5] + 1900;

$dbh->begin_work;
my $row = 0;
my $row_chunk = 1000;

foreach my $line (<>) {

    $row++;

    # Fix the 's gap -- addons do this, grr
    $line =~ s/ 's/'s/g;

    # Strip random junk chars
    $line =~ s/[\x80-\xFF]//g;

    # Strip trailing spaces
    $line =~ s/\s+$//;

    # Work out time stuff
    my @parts = split(/  /, $line);
    my ($ts, $ms) = split(/\./, $parts[0]);
    $ts =~ s/^(\d+)\/(\d+) (\d{2}:\d{2}:\d{2})/$year-$1-$2 $3/;
    $ts =~ s/$year-(\d)-/$year-0$1-/;
    $ts =~ s/$year-(\d\d)-(\d) /$year-$1-0$2 /;

    my $match_flag = 0;

    foreach my $match (keys(%loglines)) {
        next unless (
                $match_flag == 0
                && $parts[1] =~ /^$match$/ 
                && $loglines{$match}
                );
        $match_flag = 1;
        my $sql = eval $loglines{$match};
        if (! $debug) {
            my $sth = $dbh->prepare($sql) || die $sql;
            $sth->execute || die;
            if ($row % $row_chunk == 0) {
                $dbh->commit;
                print "Row $row; committing.\n";
                $dbh->begin_work;
            }
        } 
        else {
            print "RAW:    ", $line, "\n";
            print "REGEXP: ", $match, "\n";
            print "SQL:    ", $sql, "\n";
            print "\n";
        }
    }
}

if ($row % $row_chunk != 0) {
    $dbh->commit;
}

#### Subroutines
####

sub sqlize {
    my (%args) = @_;
    my $sql = 
        "INSERT INTO " . $args{'table'} . " (" .
            join(", ", map { $dbh->quote($_) } @{$args{'cols'}}) .
        ") " .
        "VALUES (" .
            join(", ", map { $dbh->quote($_) } @{$args{'vals'}}) .
        ")";
    return $sql;
}
